package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.db.domain.system.MpCashApply;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.web.MpIncomeRecord;
import com.ruoyi.db.mapper.system.MpCashApplyMapper;
import com.ruoyi.db.mapper.system.MpClientMapper;
import com.ruoyi.db.mapper.web.MpIncomeRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IMpCashApplyService;

import javax.transaction.Transactional;

/**
 * 用户申请提现记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-23
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class MpCashApplyServiceImpl implements IMpCashApplyService 
{
    @Autowired
    private MpCashApplyMapper mpCashApplyMapper;
    @Autowired
    private MpClientMapper mpClientMapper;
    @Autowired
    private MpIncomeRecordMapper mpIncomeRecordMapper;

    /**
     * 查询用户申请提现记录
     * 
     * @param cashApplyId 用户申请提现记录主键
     * @return 用户申请提现记录
     */
    @Override
    public MpCashApply selectMpCashApplyByCashApplyId(Long cashApplyId)
    {
        return mpCashApplyMapper.selectMpCashApplyByCashApplyId(cashApplyId);
    }

    /**
     * 查询用户申请提现记录列表
     * 
     * @param mpCashApply 用户申请提现记录
     * @return 用户申请提现记录
     */
    @Override
    public List<MpCashApply> selectMpCashApplyList(MpCashApply mpCashApply)
    {
        return mpCashApplyMapper.selectMpCashApplyList(mpCashApply);
    }

    /**
     * 新增用户申请提现记录
     * 
     * @param mpCashApply 用户申请提现记录
     * @return 结果
     */
    @Override
    public int insertMpCashApply(MpCashApply mpCashApply)
    {
        return mpCashApplyMapper.insertMpCashApply(mpCashApply);
    }

    /**
     * 修改用户申请提现记录
     * 
     * @param mpCashApply 用户申请提现记录
     * @return 结果
     */
    @Override
    public int updateMpCashApply(MpCashApply mpCashApply)
    {
        if(mpCashApply.getApprovalStatus().equals("103")){
            //返回用户提现金额与费率
            MpCashApply old = mpCashApplyMapper.selectMpCashApplyByCashApplyId(mpCashApply.getCashApplyId());
            MpClient client = mpClientMapper.selectMpClientByClientId(old.getClientId());
            BigDecimal goldCash = old.getGoldCashAmount();
            BigDecimal silverCash = old.getSilverCashAmount();
            BigDecimal taxCash = old.getTaxAmount();
            if(goldCash!=null){
                client.setGoldBeanBalance(client.getGoldBeanBalance().add(goldCash).add(taxCash).add(mpCashApply.getApplyAmount()));
                mpClientMapper.updateMpClient(client);
                MpIncomeRecord recordReturn = new MpIncomeRecord();
                recordReturn.setClientId(client.getClientId());
                recordReturn.setCreateTime(new Date());
                recordReturn.setIsIncome(1l);
                recordReturn.setAmount(goldCash.add(taxCash).add(mpCashApply.getApplyAmount()));
                recordReturn.setContent("用户提现失败金藻返还");
                recordReturn.setType(1l);
                recordReturn.setCurrentBalance(client.getGoldBeanBalance());
                mpIncomeRecordMapper.insertMpIncomeRecord(recordReturn);
            }
            if(silverCash!=null){
                client.setGoldBeanBalance(client.getGoldBeanBalance().add(taxCash).add(mpCashApply.getApplyAmount()));
                client.setSilverBeanBalance(client.getSilverBeanBalance().add(silverCash));
                mpClientMapper.updateMpClient(client);
                MpIncomeRecord recordReturn = new MpIncomeRecord();
                recordReturn.setClientId(client.getClientId());
                recordReturn.setCreateTime(new Date());
                recordReturn.setIsIncome(1l);
                recordReturn.setAmount(goldCash.add(taxCash).add(mpCashApply.getApplyAmount()));
                recordReturn.setContent("用户提现失败金藻返还");
                recordReturn.setType(1l);
                recordReturn.setCurrentBalance(client.getGoldBeanBalance());
                mpIncomeRecordMapper.insertMpIncomeRecord(recordReturn);
                MpIncomeRecord recordReturn1 = new MpIncomeRecord();
                recordReturn1.setClientId(client.getClientId());
                recordReturn1.setCreateTime(new Date());
                recordReturn1.setIsIncome(1l);
                recordReturn1.setAmount(silverCash);
                recordReturn1.setContent("用户提现银藻手续费返还");
                recordReturn1.setType(2l);
                recordReturn1.setCurrentBalance(client.getSilverBeanBalance());
                mpIncomeRecordMapper.insertMpIncomeRecord(recordReturn1);
            }
        }
        return mpCashApplyMapper.updateMpCashApply(mpCashApply);
    }

    /**
     * 批量删除用户申请提现记录
     * 
     * @param cashApplyIds 需要删除的用户申请提现记录主键
     * @return 结果
     */
    @Override
    public int deleteMpCashApplyByCashApplyIds(Long[] cashApplyIds)
    {
        return mpCashApplyMapper.deleteMpCashApplyByCashApplyIds(cashApplyIds);
    }

    /**
     * 删除用户申请提现记录信息
     * 
     * @param cashApplyId 用户申请提现记录主键
     * @return 结果
     */
    @Override
    public int deleteMpCashApplyByCashApplyId(Long cashApplyId)
    {
        return mpCashApplyMapper.deleteMpCashApplyByCashApplyId(cashApplyId);
    }

    @Override
    public void updateMpCashApplys(List<MpCashApply> mpCashApplys) {
        for (MpCashApply a:mpCashApplys) {
            a.setApprovalStatus("102");
            mpCashApplyMapper.updateMpCashApply(a);
        }
    }
}
